package create;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Method;

public class CreateMainConsumer {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// File dir = new
		// File("/Users/jiangguoshun/Desktop/project/workspace-sts-3.6.4.RELEASE/osgit/jilefojing-net-project-soa/jilefojing-integration/src/main/java/com/jilefojing/service");
		File dir = new File("/Users/jiangguoshun/Desktop/project/workspace-sts-3.6.4.RELEASE/osgit/jilefojing-net-project-soa/jilefojing-integration/src/main/java/com/jilefojing/service");

		File[] ints = dir.listFiles();
		for (File _File : ints) {
			if (_File.isFile()) {
				String useName = _File.getName().substring(1).replace(".java", "");
				String fName = useName.substring(0, 1).toLowerCase() + useName.substring(1);

				String uName = "";
				String lName = fName.substring(0, 1);
				for (int i = 0; i < fName.length(); i++) {
					String word = fName.charAt(i) + "";
					if (word.matches("[A-Z]")) {
						uName += "_" + word.toLowerCase();
						lName += word.toLowerCase();
					} else {
						uName += word;
					}
				}
				// System.out.println(uName);
				// System.out.println(fName);
				// System.out.println(useName);

				Class ic = Class.forName("com.jilefojing.service.I" + useName);
				Method[] ms = ic.getMethods();

				String oms = "";

				for (Method m : ms) {
					String mn = m.getName();
					if (mn.startsWith("save") || mn.startsWith("add") || mn.startsWith("insert") || mn.startsWith("update") || mn.startsWith("edit") || mn.startsWith("register")
							|| mn.startsWith("delete") || mn.startsWith("clean") || mn.startsWith("cancle")) {
						oms += "		<dubbo:method name=\"" + mn + "\" retries=\"0\" />\n";
					}
				}

				String temp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n"
						+ "	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tx=\"http://www.springframework.org/schema/tx\"\n"
						+ "	xmlns:context=\"http://www.springframework.org/schema/context\"\n"
						+ "	xmlns:mvc=\"http://www.springframework.org/schema/mvc\" xmlns:aop=\"http://www.springframework.org/schema/aop\"\n"
						+ "	xmlns:util=\"http://www.springframework.org/schema/util\" xmlns:dubbo=\"http://code.alibabatech.com/schema/dubbo\"\n"
						+ "	xsi:schemaLocation=\"http://www.springframework.org/schema/beans    \n" + "    http://www.springframework.org/schema/beans/spring-beans.xsd    \n"
						+ "    http://www.springframework.org/schema/tx    \n" + "    http://www.springframework.org/schema/tx/spring-tx.xsd   \n"
						+ "    http://www.springframework.org/schema/context   \n" + "    http://www.springframework.org/schema/context/spring-context.xsd   \n"
						+ "    http://www.springframework.org/schema/mvc   \n" + "    http://www.springframework.org/schema/mvc/spring-mvc.xsd\n" + "    http://www.springframework.org/schema/aop \n"
						+ "	http://www.springframework.org/schema/aop/spring-aop.xsd\n" + "	http://www.springframework.org/schema/util \n"
						+ "	http://www.springframework.org/schema/util/spring-util.xsd\n" + "	http://code.alibabatech.com/schema/dubbo\n"
						+ "    http://code.alibabatech.com/schema/dubbo/dubbo.xsd\">\n" + "\n" + "	<dubbo:reference id=\"" + fName + "\"\n" + "		interface=\"com.jilefojing.service.I" + useName
						+ "\" version=\"1.0\" url=\"dubbo://localhost:20880\" />\n" + "</beans>   ";

				// String odir = new String(
				// "/Users/jiangguoshun/Desktop/project/workspace-sts-3.6.4.RELEASE/osgit/jilefojing-net-project-soa/jilefojing-conf/src/main/resources/conf/consumer/dev/com/jilefojing/service/");

				String odir = new String(
						"/Users/jiangguoshun/Desktop/project/workspace-sts-3.6.4.RELEASE/osgit/jilefojing-net-project-soa/jilefojing-conf/src/main/resources/conf/consumer/dev/com/jilefojing/service/");

				File oFile = new File(odir + uName + "_consumer.xml");
				if (!oFile.exists()) {
					oFile.createNewFile();
					PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8"));
					pw.write(temp);
					pw.flush();
					pw.close();
				}
			}
		}

	}
}
